﻿using Infrastructure;
using Microsoft.AspNetCore.SignalR;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZR.DataCollection.Model;
using ZR.DataCollection.OperationCore.OperationEnum;
using ZR.DataCollection.OperationCore.OperationHelper;
using ZR.DataCollection.SingleParameter;
using ZR.Model.Models.ProductionControl;
using ZR.Service.IService.ProductionControl;
using ZR.ServiceCore.Signalr;

namespace ZR.DataCollection.OperationCore.OperationMode.OP40
{
    /// <summary>
    /// 请求加工
    /// </summary>
    public class ProcessOP40 : OperationInfoBase
    {
        //1上工位OK；
        //2上工位不存在；
        //3上工位NG；
        //4本工位OK；
        //5本工位NG；
        //6首件/点检/标样；
        //7连续NG；
        //8累计NG；
        //10=产品空条码；
        private Int16 collectStatus = 1;

        public override event Action<string> Event_Notification;

        private readonly IWorkstationConfigService _workstationConfigService = App.GetRequiredService<IWorkstationConfigService>();
        private readonly IStationVerificationConfigService _stationVerificationConfigService = App.GetRequiredService<IStationVerificationConfigService>();
        private readonly IStationWorkInfoService _stationWorkInfoService = App.GetRequiredService<IStationWorkInfoService>();
        private readonly IHubContext<MessageHub> _hubContext = App.GetRequiredService<IHubContext<MessageHub>>();

        public ProcessOP40(int operationType, string stationType) : base(operationType, stationType)
        {
        }

        public override void WorkDetail(ImpParameter imp)
        {
            DataDock? dataDock = imp.objectParam as DataDock;
            var keyValuePairs = CurrentCruxPoint(dataDock.LineCode, dataDock.ProductType, dataDock.ItemNo);
            SpotHandle(imp, keyValuePairs);
        }

        public override void SpotHandle(ImpParameter parameter, Dictionary<int, List<OperationPointConfig>> SpotGroup)
        {
            foreach (var SpotInfo in SpotGroup)
            {
                switch (SpotInfo.Key)
                {
                    case (int)DataOperateEnum.Read:
                        ReadFunction(parameter, SpotInfo.Value);
                        break;

                    case (int)DataOperateEnum.Write:
                        SpotInfo.Value.ForEach(x =>
                        {
                            if (collectStatus.Equals(1))
                            {
                                parameter.PLCDetail.WriteValue(x.ItemNo, collectStatus);
                                _hubContext.Clients.All.SendAsync($"OP40Allow", "允许加工");
                            }
                            else if (collectStatus.Equals(2))
                            {
                                parameter.PLCDetail.WriteValue(x.ItemNo, collectStatus);
                                _hubContext.Clients.All.SendAsync($"OP40Allow", "不允许加工");
                            }
                            else if (collectStatus.Equals(3))
                            {
                                parameter.PLCDetail.WriteValue(x.ItemNo, collectStatus);
                                _hubContext.Clients.All.SendAsync($"OP40Allow", "不允许加工");
                            }
                            else if (collectStatus.Equals(4))
                            {
                                parameter.PLCDetail.WriteValue(x.ItemNo, collectStatus);
                                _hubContext.Clients.All.SendAsync($"OP40Allow", "不允许加工");
                            }
                            else if (collectStatus.Equals(5))
                            {
                                parameter.PLCDetail.WriteValue(x.ItemNo, collectStatus);
                                _hubContext.Clients.All.SendAsync($"OP40Allow", "不允许加工");
                            }
                            else if (collectStatus.Equals(6))
                            {
                                parameter.PLCDetail.WriteValue(x.ItemNo, collectStatus);
                                _hubContext.Clients.All.SendAsync($"OP40Allow", "不允许加工");
                            }
                            else if (collectStatus.Equals(7))
                            {
                                parameter.PLCDetail.WriteValue(x.ItemNo, collectStatus);
                                _hubContext.Clients.All.SendAsync($"OP40Allow", "不允许加工");
                            }
                            else if (collectStatus.Equals(8))
                            {
                                parameter.PLCDetail.WriteValue(x.ItemNo, collectStatus);
                                _hubContext.Clients.All.SendAsync($"OP40Allow", "不允许加工");
                            }
                            else if (collectStatus.Equals(10))
                            {
                                parameter.PLCDetail.WriteValue(x.ItemNo, collectStatus);
                                _hubContext.Clients.All.SendAsync($"OP40Allow", "不允许加工");
                            }
                            else
                            {
                                parameter.PLCDetail.WriteValue(x.ItemNo, Convert.ToInt16(0));
                            }
                        });
                        break;

                    case (int)DataOperateEnum.Verify:
                        break;

                    default:
                        break;
                }
            }
        }

        private void ReadFunction(ImpParameter parameter, List<OperationPointConfig> OperationPointList)
        {
            try
            {
                DataDock dataDock = parameter.objectParam as DataDock;
                foreach (var PointInfo in OperationPointList)
                {
                    string? productId = parameter.PLCDetail.ReadValue(PointInfo.ItemNo).ToString();
                    _hubContext.Clients.All.SendAsync($"OP40BarCode", productId);
                    if (string.IsNullOrWhiteSpace(productId))//产品空条码
                    {
                        collectStatus = 10;
                        return;
                    }
                    StationWorkInfo stationWorkInfo = null;
                    bool sve = _workstationConfigService.QueryInfo(dataDock?.LineCode, dataDock?.StationCode).StationVerifyEnabled.Equals(1);
                    if (sve)
                    {
                        string upperStation = _stationVerificationConfigService.UpperWorkstation(dataDock.LineCode, dataDock.StationCode);
                        stationWorkInfo = _stationWorkInfoService.QueryInfo(dataDock.LineCode, upperStation, productId);
                        if (stationWorkInfo == null)//上工位信息
                        {
                            collectStatus = 2;
                            return;
                        }
                        if (stationWorkInfo.WorkResult.Equals("NG"))//上工位结果为NG
                        {
                            collectStatus = 3;
                            return;
                        }
                    }
                    //OverStationStart(parameter);
                    //ProductInfoManager.Instance.SetOrderCode(dataDock.StationCode, stationWorkInfo.OrderCode);
                    //ProductInfoManager.Instance.SetProductNo(dataDock.StationCode, productId);
                    //ProductInfoManager.Instance.SetStartTime(dataDock.StationCode, DateTime.Now);
                }
            }
            catch (Exception)
            {
            }
        }
    }
}